/**
 * @author Tsering Yuu
 * @description params must be page & size(not pageSize)
 * @summary table common logic
 */

import CustomTable from '../components/custom-table';
import { mapState, mapActions } from 'vuex';

export default function ({
  searchFuncName,
  storeModuleName,
  storeListField,
  storeTotalField = 'total',
  storeUpdateFuncName,
}) {
  return {
    components: {
      CustomTable,
    },
    computed: {
      ...mapState(storeModuleName, [storeListField, storeTotalField]),
    },
    data () {
      return {
        page: 1,
        pageSize: 10,
      };
    },
    mounted () {
      this.search();
    },
    methods: {
      ...mapActions(storeModuleName, [storeUpdateFuncName]),
      search () {
        this[searchFuncName]({
          page: this.page,
          size: this.pageSize,
        });
      },
      onPageChange (currentPage) {
        this.page = currentPage;
        this.search();
      },
      onPageSizeChange (newSize) {
        this.page = 1; // 容错代码
        this.pageSize = newSize;
        this.search();
      },
    },
  };
};
